/*
 * session.cpp
 *
 *  Created on: Aug 1, 2013
 *      Author: killerg
 */
#include "server_deps.h"
#include "client_session.h"
#include "server.h"
#include "server_console.h"
#include "server_cfg.h"

#include "share/db_mgr.h"
#include "share/net_s.h"
#include "share/connection_mgr.h"
#include "share/clock.h"
#include "share/msg.h"
#include "share/profiler.h"
#include "share/msg_coder.h"

#include "game_logic/player_data.h"
#include "game_logic/player_mgr.h"
//#include "game_logic/mail_mgr.h"

#include "share/msg_handler.h"
#define g_cmsg_handler Singleton<msg_handler_t<cs::CMsg>>::Instance()


void client_session_t::on_connect() {
    stage = ESConnedted;
    stat.reset();
    stat.conn_start = g_clock->tt_now;
}

void client_session_t::on_disconnect() {
    if (NULL != player) {
        player->on_logoff();
    }
    stat.conn_end = g_clock->tt_now;
    stage = ESInvalid;
    stat.post_save();
}

void handle_default(client_session_t* sess, cs::CMsg* msg) {
    UTIL_LOG(E, "msg not deal %d", msg->id());
    sess->_trans->disconnect();
}

void handle_cecho               (client_session_t* sess, cs::CMsg* msg);
void handle_cload               (client_session_t* sess, cs::CMsg* msg);
void handle_ccreate             (client_session_t* sess, cs::CMsg* msg);
void handle_ccreatepet          (client_session_t* sess, cs::CMsg* msg);
void handle_ccombat             (client_session_t* sess, cs::CMsg* msg);
void handle_cgm                 (client_session_t* sess, cs::CMsg* msg);
void handle_csellitem           (client_session_t* sess, cs::CMsg* msg);

void handle_cpetname            (client_session_t* sess, cs::CMsg* msg);
void handle_cpetfeed            (client_session_t* sess, cs::CMsg* msg);
void handle_cpetcharacter       (client_session_t* sess, cs::CMsg* msg);
void handle_cpetrelease         (client_session_t* sess, cs::CMsg* msg);

void handle_cventurecombat      (client_session_t* sess, cs::CMsg* msg);
void handle_cventurebosscombat  (client_session_t* sess, cs::CMsg* msg);
void handle_cventurebossreset   (client_session_t* sess, cs::CMsg* msg);
void handle_cexploreenter       (client_session_t* sess, cs::CMsg* msg);
void handle_cexplorebuyitem     (client_session_t* sess, cs::CMsg* msg);
void handle_cexploreact         (client_session_t* sess, cs::CMsg* msg);

void handle_cpetcombatpos       (client_session_t* sess, cs::CMsg* msg);
void handle_cpetrear            (client_session_t* sess, cs::CMsg* msg);
void handle_cpetevo             (client_session_t* sess, cs::CMsg* msg);

void handle_cpetskilllearn      (client_session_t* sess, cs::CMsg* msg);
void handle_cpetskillforget     (client_session_t* sess, cs::CMsg* msg);
void handle_cpetskilllevelup    (client_session_t* sess, cs::CMsg* msg);
void handle_cunlocknskillslot   (client_session_t* sess, cs::CMsg* msg);

void handle_ceggrandom          (client_session_t* sess, cs::CMsg* msg);
void handle_cegghatch           (client_session_t* sess, cs::CMsg* msg);
void handle_chatcheggunlock     (client_session_t* sess, cs::CMsg* msg);


void handle_cplayermedal        (client_session_t* sess, cs::CMsg* msg);
void handle_cequiplevelup       (client_session_t* sess, cs::CMsg* msg);
void handle_cequipenhance       (client_session_t* sess, cs::CMsg* msg);
void handle_cequipstar          (client_session_t* sess, cs::CMsg* msg);

void handle_cbagextend          (client_session_t* sess, cs::CMsg* msg);
void handle_cbagsell            (client_session_t* sess, cs::CMsg* msg);
void handle_cbaguse             (client_session_t* sess, cs::CMsg* msg);
void handle_cbagprepare         (client_session_t* sess, cs::CMsg* msg);


void handle_cspriteexplore      (client_session_t* sess, cs::CMsg* msg);
void handle_cspritesummon       (client_session_t* sess, cs::CMsg* msg);
void handle_cspritedecompose    (client_session_t* sess, cs::CMsg* msg);
void handle_cspriteskilllevelup (client_session_t* sess, cs::CMsg* msg);
void handle_cspriterelease      (client_session_t* sess, cs::CMsg* msg);
void handle_cspriterest         (client_session_t* sess, cs::CMsg* msg);
void handle_coccuptionstatus    (client_session_t* sess, cs::CMsg* msg);
void handle_coccupy             (client_session_t* sess, cs::CMsg* msg);
void handle_ccomposestone       (client_session_t* sess, cs::CMsg* msg);
void handle_ccomposeskill       (client_session_t* sess, cs::CMsg* msg);
void handle_ccomposemagic       (client_session_t* sess, cs::CMsg* msg);
void handle_cfricombat          (client_session_t* sess, cs::CMsg* msg);
void handle_cfrigift            (client_session_t* sess, cs::CMsg* msg);
void handle_cfriremove          (client_session_t* sess, cs::CMsg* msg);
void handle_cfriadd             (client_session_t* sess, cs::CMsg* msg);
void handle_cfrirefresh         (client_session_t* sess, cs::CMsg* msg);
void handle_cfrifind            (client_session_t* sess, cs::CMsg* msg);
void handle_cplayerinfo         (client_session_t* sess, cs::CMsg* msg);
void handle_carenarefresh       (client_session_t* sess, cs::CMsg* msg);
void handle_carenacombat        (client_session_t* sess, cs::CMsg* msg);
void handle_cranklist           (client_session_t* sess, cs::CMsg* msg);
void handle_carenagetreward     (client_session_t* sess, cs::CMsg* msg);
void handle_csimpleplayerinfo   (client_session_t* sess, cs::CMsg* msg);

void handle_cmaillist           (client_session_t* sess, cs::CMsg* msg);
void handle_cmailread           (client_session_t* sess, cs::CMsg* msg);
void handle_cmailsend           (client_session_t* sess, cs::CMsg* msg);
void handle_cmaildel            (client_session_t* sess, cs::CMsg* msg);
void handle_cmailreward         (client_session_t* sess, cs::CMsg* msg);
void handle_cchat               (client_session_t* sess, cs::CMsg* msg);
void handle_ctaskreward         (client_session_t* sess, cs::CMsg* msg);
void handle_calivereward        (client_session_t* sess, cs::CMsg* msg);
void handle_cchatcombat         (client_session_t* sess, cs::CMsg* msg);
void handle_cpetinfo            (client_session_t* sess, cs::CMsg* msg);
void handle_cdailygiftget       (client_session_t* sess, cs::CMsg* msg);
void handle_cdailyvipgiftget    (client_session_t* sess, cs::CMsg* msg);
void handle_cmallbuy            (client_session_t* sess, cs::CMsg* msg);
void handle_cmoneyget           (client_session_t* sess, cs::CMsg* msg);
void handle_cluckystar          (client_session_t* sess, cs::CMsg* msg);
void handle_cbuyarenacombatnum  (client_session_t* sess, cs::CMsg* msg);
void handle_coccupationnum      (client_session_t* sess, cs::CMsg* msg);
void handle_cusecode            (client_session_t* sess, cs::CMsg* msg);
void handle_cworldbosscombat    (client_session_t* sess, cs::CMsg* msg);
void handle_cworldbossdata      (client_session_t* sess, cs::CMsg* msg);
void handle_cweeklycntreward	(client_session_t* sess, cs::CMsg* msg);
void handle_cdailypower    		(client_session_t* sess, cs::CMsg* msg);
void handle_cunlockwatch		(client_session_t* sess, cs::CMsg* msg);
void handle_cfriendgift 		(client_session_t* sess, cs::CMsg* msg);
void handle_ctrain		 		(client_session_t* sess, cs::CMsg* msg);
void handle_cfloorreset	 		(client_session_t* sess, cs::CMsg* msg);
void handle_cfloorcombat 		(client_session_t* sess, cs::CMsg* msg);
void handle_cfloorreward    	(client_session_t* sess, cs::CMsg* msg);
void handle_frivalue	    	(client_session_t* sess, cs::CMsg* msg);
void handle_cfristageup	    	(client_session_t* sess, cs::CMsg* msg);
void handle_cfristagewash    	(client_session_t* sess, cs::CMsg* msg);
void handle_cspriteformlevelup	(client_session_t* sess, cs::CMsg* msg);
void handle_cspriteformrefresh  (client_session_t* sess, cs::CMsg* msg);
void handle_cpointrankopen  	(client_session_t* sess, cs::CMsg* msg);
void handle_cpointrankcombat  	(client_session_t* sess, cs::CMsg* msg);
void handle_ccardreward		  	(client_session_t* sess, cs::CMsg* msg);
void handle_cpetinherit		  	(client_session_t* sess, cs::CMsg* msg);
void handle_cseconddayreward 	(client_session_t* sess, cs::CMsg* msg);


void client_session_t::update() {
    online_time += g_clock->delta;

    if (stage <= ESConnedted) {
        if (online_time >= 20) {
            _trans->disconnect();
            UTIL_LOG(T, "client doesn't auth in 20 seconds so kicked");
        }
    }

    if (player) {
        player->update();

//        if (player->att(Pl4n_PersonMailNew) < 0 || player->att(Pl4n_SystemMailNew) < 0)
//            ++g_mail->loadmail;
    }

    ++g_svr.count[stage];
}

bool client_session_t::send_msg(cs::SMsg* &msg) {
    return g_sessmgr->send_msg(this, msg);
}

void client_session_t::send_notify(const char* fmt, ...) {
    if (NULL == this->_trans)
        return;

    va_list args;
    va_start(args, fmt);
    static char buffer[1024];
    vsprintf(buffer, fmt, args);
    va_end(args);

    CREATE_CS_SMSG(SNotify, notify);
    notify->set_content(buffer);
    send_msg(___msg);
}

bool clientsess_mgr_t::init() {
    bool ok = base::init(
            "0.0.0.0",
            g_cfg->ListenClientPort,
            10000,
            5,
            60 * 20,
            2048
            );
    if (g_cfg->DecodeClient)
        base::net.code_cb = encmsg;

    if (!ok) {
        UTIL_LOG(E, "listening for client at %s:%d failed",
                g_cfg->ListenClientIp.c_str(),
                g_cfg->ListenClientPort);
        return false;
    }
    UTIL_LOG(I, "listening for client at %s:%d",
            g_cfg->ListenClientIp.c_str(),
            g_cfg->ListenClientPort);

    ok = ok && base::reg(cs::ID_CEcho               , handle_cecho              );
    ok = ok && base::reg(cs::ID_CLoad               , handle_cload              );
    ok = ok && base::reg(cs::ID_CCreate             , handle_ccreate            );
    ok = ok && base::reg(cs::ID_CCreatePet          , handle_ccreatepet         );
    ok = ok && base::reg(cs::ID_CCombat             , handle_ccombat            );
    ok = ok && base::reg(cs::ID_CGM                 , handle_cgm                );
    ok = ok && base::reg(cs::ID_CSellItem           , handle_csellitem          );
    ok = ok && base::reg(cs::ID_CPetRename          , handle_cpetname           );
    ok = ok && base::reg(cs::ID_CPetFeed            , handle_cpetfeed           );
    ok = ok && base::reg(cs::ID_CPetCharacter       , handle_cpetcharacter      );
    ok = ok && base::reg(cs::ID_CPetRelease         , handle_cpetrelease        );
    ok = ok && base::reg(cs::ID_CVentureCombat      , handle_cventurecombat     );
    ok = ok && base::reg(cs::ID_CVentureBossCombat  , handle_cventurebosscombat );
    ok = ok && base::reg(cs::ID_CVentureBossReset   , handle_cventurebossreset  );
    ok = ok && base::reg(cs::ID_CExploreEnter       , handle_cexploreenter      );
    ok = ok && base::reg(cs::ID_CExploreBuyItem     , handle_cexplorebuyitem    );
    ok = ok && base::reg(cs::ID_CExploreAct         , handle_cexploreact        );
    ok = ok && base::reg(cs::ID_CPetCombatPos       , handle_cpetcombatpos      );
    ok = ok && base::reg(cs::ID_CPetRear            , handle_cpetrear           );
    ok = ok && base::reg(cs::ID_CPetEvo             , handle_cpetevo            );
    ok = ok && base::reg(cs::ID_CPetSkillLearn      , handle_cpetskilllearn     );
    ok = ok && base::reg(cs::ID_CPetSkillForget     , handle_cpetskillforget    );
    ok = ok && base::reg(cs::ID_CPetSkillLevelUp    , handle_cpetskilllevelup   );
    ok = ok && base::reg(cs::ID_CEggRandom          , handle_ceggrandom         );
    ok = ok && base::reg(cs::ID_CEggHatch           , handle_cegghatch          );
    ok = ok && base::reg(cs::ID_CPlayerMedal        , handle_cplayermedal       );
    ok = ok && base::reg(cs::ID_CEquipLevelUp       , handle_cequiplevelup      );
    ok = ok && base::reg(cs::ID_CEquipEnhance       , handle_cequipenhance      );
    ok = ok && base::reg(cs::ID_CEquipStar          , handle_cequipstar         );
    ok = ok && base::reg(cs::ID_CBagExtend          , handle_cbagextend         );
    ok = ok && base::reg(cs::ID_CBagSell            , handle_cbagsell           );
    ok = ok && base::reg(cs::ID_CBagUse             , handle_cbaguse            );
    ok = ok && base::reg(cs::ID_CBagPrepare         , handle_cbagprepare        );
    ok = ok && base::reg(cs::ID_CSpriteExplore      , handle_cspriteexplore     );     // 探索
    ok = ok && base::reg(cs::ID_CSpriteSummon       , handle_cspritesummon      );      // 召唤
    ok = ok && base::reg(cs::ID_CSpriteDecompose    , handle_cspritedecompose   );      // 分解
    ok = ok && base::reg(cs::ID_CSpriteSkillLevelup , handle_cspriteskilllevelup);      // 升级技能
    ok = ok && base::reg(cs::ID_CSpriteRelease      , handle_cspriterelease     );      // 放生
    ok = ok && base::reg(cs::ID_CSpriteRest         , handle_cspriterest        );      // 出战休息
    ok = ok && base::reg(cs::ID_COccuptionStatus    , handle_coccuptionstatus   );
    ok = ok && base::reg(cs::ID_COccupy             , handle_coccupy            );
    ok = ok && base::reg(cs::ID_CComposeStone       , handle_ccomposestone      );
    ok = ok && base::reg(cs::ID_CComposeSkill       , handle_ccomposeskill      );
    ok = ok && base::reg(cs::ID_CComposeMagic       , handle_ccomposemagic      );
    ok = ok && base::reg(cs::ID_CFriCombat          , handle_cfricombat         );
    ok = ok && base::reg(cs::ID_CFriGift            , handle_cfrigift           );
    ok = ok && base::reg(cs::ID_CFriRemove          , handle_cfriremove         );
    ok = ok && base::reg(cs::ID_CFriAdd             , handle_cfriadd            );
    ok = ok && base::reg(cs::ID_CFriRefresh         , handle_cfrirefresh        );
    ok = ok && base::reg(cs::ID_CFriFind            , handle_cfrifind           );
    ok = ok && base::reg(cs::ID_CPlayerInfo         , handle_cplayerinfo        );
    ok = ok && base::reg(cs::ID_CArenaRefresh       , handle_carenarefresh      );
    ok = ok && base::reg(cs::ID_CArenaCombat        , handle_carenacombat       );
    ok = ok && base::reg(cs::ID_CRankList           , handle_cranklist          );
    ok = ok && base::reg(cs::ID_CArenaGetReward     , handle_carenagetreward    );
    ok = ok && base::reg(cs::ID_CHatchEggUnlock     , handle_chatcheggunlock    );
    ok = ok && base::reg(cs::ID_CSimplePlayerInfo   , handle_csimpleplayerinfo  );
    ok = ok && base::reg(cs::ID_CMailList           , handle_cmaillist          );
    ok = ok && base::reg(cs::ID_CMailRead           , handle_cmailread          );
    ok = ok && base::reg(cs::ID_CMailSend           , handle_cmailsend          );
    ok = ok && base::reg(cs::ID_CMailDel            , handle_cmaildel           );
    ok = ok && base::reg(cs::ID_CMailReward         , handle_cmailreward        );
    ok = ok && base::reg(cs::ID_CChat               , handle_cchat              );
    ok = ok && base::reg(cs::ID_CTaskReward         , handle_ctaskreward        );
    ok = ok && base::reg(cs::ID_CAliveReward        , handle_calivereward       );
    ok = ok && base::reg(cs::ID_CChatCombat         , handle_cchatcombat        );
    ok = ok && base::reg(cs::ID_CPetInfo            , handle_cpetinfo           );
    ok = ok && base::reg(cs::ID_CDailyGiftGet       , handle_cdailygiftget      );
    ok = ok && base::reg(cs::ID_CDailyVipGiftGet    , handle_cdailyvipgiftget   );
    ok = ok && base::reg(cs::ID_CMallBuy            , handle_cmallbuy           );
    ok = ok && base::reg(cs::ID_CMoneyGet           , handle_cmoneyget          );
    ok = ok && base::reg(cs::ID_CLuckyStar          , handle_cluckystar         );
    ok = ok && base::reg(cs::ID_CUnlockNSkillSlot   , handle_cunlocknskillslot  );
    ok = ok && base::reg(cs::ID_CBuyArenaCombatNum  , handle_cbuyarenacombatnum );
    ok = ok && base::reg(cs::ID_CBuyOccupationNum   , handle_coccupationnum     );
    ok = ok && base::reg(cs::ID_CUseCode            , handle_cusecode           );
    ok = ok && base::reg(cs::ID_CWorldBossCombat    , handle_cworldbosscombat   );
    ok = ok && base::reg(cs::ID_CWorldBossData      , handle_cworldbossdata		);
    ok = ok && base::reg(cs::ID_CWeeklyCntReward    , handle_cweeklycntreward	);
    ok = ok && base::reg(cs::ID_CDailyPower	        , handle_cdailypower    	);
    ok = ok && base::reg(cs::ID_CUnlockWatch		, handle_cunlockwatch    	);
    ok = ok && base::reg(cs::ID_CFriendGift			, handle_cfriendgift    	);
    ok = ok && base::reg(cs::ID_CTrain				, handle_ctrain         	);
    ok = ok && base::reg(cs::ID_CFloorReset			, handle_cfloorreset       	);
    ok = ok && base::reg(cs::ID_CFloorCombat		, handle_cfloorcombat      	);
    ok = ok && base::reg(cs::ID_CFloorReward		, handle_cfloorreward      	);
    ok = ok && base::reg(cs::ID_CFriValue		    , handle_frivalue		    );
    ok = ok && base::reg(cs::ID_CFriStageUp		    , handle_cfristageup		);
    ok = ok && base::reg(cs::ID_CFriStageWash		, handle_cfristagewash		);
    ok = ok && base::reg(cs::ID_CSpriteFormLevelup	, handle_cspriteformlevelup );
    ok = ok && base::reg(cs::ID_CSpriteFormRefresh  , handle_cspriteformrefresh );
    ok = ok && base::reg(cs::ID_CPointRankOpen  	, handle_cpointrankopen 	);
    ok = ok && base::reg(cs::ID_CPointRankCombat  	, handle_cpointrankcombat	);
    ok = ok && base::reg(cs::ID_CCardReward		  	, handle_ccardreward		);
    ok = ok && base::reg(cs::ID_CPetInherit		  	, handle_cpetinherit		);
    ok = ok && base::reg(cs::ID_CSecondDayReward	, handle_cseconddayreward	);


    return ok;
}

const char* ServerStageName[] = {
        "Inv",
        "Cnd",
        "Nec",
        "Crg",
        "Crd",
        "Ldg",
        "Ldd",
};





